package cn.wolfcode.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class JSONResult<T> {
    public static final int CODE_SUCCESS = 200;
    public static final String MSG_SUCCESS = "成功";

    public static final int CODE_ERROR = 500;
    public static final String MSG_ERROR = "内部错误";

    public static final int CODE_ERROR_PARAM = 501;
    public static final String MSG_ERROR_PARAM = "参数错误";

    public static final int CODE_NOLOGIN = 401;
    public static final String MSG_NOLOGIN = "未登录";

    private int code;
    private String msg;
    private T data;

    public JSONResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> JSONResult<T> success(T data){
        return new JSONResult<T>(CODE_SUCCESS, MSG_SUCCESS, data);
    }

    public static JSONResult success(){
        return new JSONResult(CODE_SUCCESS, MSG_SUCCESS, null);
    }

    public static <T> JSONResult error(int code,String msg,T data){
        return new JSONResult(CODE_ERROR, msg, data);
    }

    public static JSONResult defaultError(){
        return new JSONResult(CODE_ERROR, MSG_ERROR, null);
    }

    public static JSONResult noLoginError(){
        return new JSONResult(CODE_NOLOGIN, MSG_NOLOGIN, null);
    }

    public static JSONResult paramError(String msg){
        return new JSONResult(CODE_ERROR_PARAM, msg, null);
    }
}
